package com.cheng.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cheng.common.domain.BaseSearchVO;
import com.cheng.common.domain.R;
import com.cheng.system.model.Role;
import com.cheng.system.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    //更新角色的时候也需要更新session中的角色
    @Autowired
    RoleService roleService;

    @GetMapping("save")
    public R<Boolean> save(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
        return R.ok(true);
    }

    @GetMapping("delete")
    public R<Boolean> delete(@RequestParam Long id) {
        roleService.removeById(id);
        return R.ok(true);
    }

    @GetMapping("list")
    public R<PageInfo<Role>> list(BaseSearchVO searchVO) {
        PageHelper.startPage(searchVO.getPageNum(), searchVO.getPageSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(searchVO.getKeyword())) {
            queryWrapper.like(Role::getRoleKey, searchVO.getKeyword()).or()
                    .like(Role::getRoleName, searchVO.getKeyword());
        }
        List<Role> list = roleService.list(queryWrapper);
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        return R.ok(rolePageInfo);
    }

    @GetMapping("get")
    public R<Role> get(@RequestParam Long id) {
        return R.ok(roleService.getById(id));
    }
}

